<template>
	<div class="flowable-modeler-container">
		<el-card shadow="hover" class="mb10" v-auth="'flowable:modeler:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.modelName" placeholder="请输入模板名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-table ref="tableModelerUndeployRef" :data="tableData.data" style="width: 100%" row-key="id">
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="缩略图" width="120">
					<template #default="scope">
						<el-image style="width: 90px; height: 30px;"
							:src="scope.row.diagramUrl"
							:preview-teleported="true"
							:preview-src-list="[scope.row.diagramUrl]">
						</el-image>
					</template>
				</el-table-column>
				<el-table-column label="模板名称" prop="modelName" show-overflow-tooltip></el-table-column>
				<el-table-column label="模板标识" prop="modelKey" show-overflow-tooltip></el-table-column>
				<el-table-column label="版本号" prop="version" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建者" prop="createBy" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="125" v-auths="['flowable:modeler:deploy']">
					<template #default="scope">
						<el-button v-auth="'flowable:modeler:deploy'" type="primary" link @click="onDeployModeler(scope.row)">部署</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getModelerUndeployList, deployModeler, getModelerDiagram } from '/@/api/flowable/modeler/index';
export default {
	name: 'flowable/modeler/undeploy',
	components: { },
	setup() {
		const tableModelerUndeployRef = ref();
		const state = reactive({
			search:{
				modelName: ''
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getModelerUndeployList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = [];
					for(let i = 0; i < res.data.length; i++){
						loadModelerDiagram(res.data[i].id).then(url =>{
							res.data[i].diagramUrl = url;
							state.tableData.data.push(res.data[i]);
						});
					}
					state.tableData.total = res.total;
				}
			});
		}
		// 加载预览流程图
		const loadModelerDiagram = async (modelKey) => {
			const blob = await getModelerDiagram(modelKey);
			const url = window.URL.createObjectURL(blob);
			return url;
		}
		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.modelName = '';
			loadTableData(state.search);
		}
		//部署模板
		const onDeployModeler = (row: object) => {
			ElMessageBox.prompt('', '模板部署', {
				inputPlaceholder: '请输入模板部署的流程类别',
				confirmButtonText: '部署',
				cancelButtonText: '取消',
				inputValidator: (value) => {
					if(!value){
						return '模板部署的流程类别不能为空'
					}
					return true;
				}
			}).then(({ value }) => {
				deployModeler({modelId: row.id, category: value}).then((res) => {
					if(res.code === 0){
						ElMessage.success('部署成功');
						loadTableData({});
					}
				});
			}).catch(() => {
				
			});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData({});
		});
		return {
			tableModelerUndeployRef,
			onTableSearch,
			onResetSearch,
			onDeployModeler,
			...toRefs(state),
		};
	},
};
</script>